package com.augurit.gzcgwsp.lhsp.web;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.web.struts2.Struts2Utils;

import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.common.base.action.ExtCrudActionSupport;
import com.augurit.common.system.web.auth.form.LoginUserForm;
import com.augurit.common.util.ExtUtils;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.gzcgwsp.lhsp.service.IProcessInstanceService;
import com.augurit.gzcgwsp.lhsp.web.form.LhspSendItemNoticeForm;
import com.augurit.wf.bi.service.IWfBusTemplateService;
import com.augurit.wf.bi.web.form.WfBusInstanceTaskForm;
import com.augurit.wf.bi.web.form.WfBusTemplateForm;
import com.augurit.wf.common.service.IWfService;
import com.augurit.wf.common.util.WfUtils;

@SuppressWarnings("serial")
@Results({
    @Result(name = CrudActionSupport.SUCCESS, location = "/gzcgwsp/ywcmn/lhsp/lhsp.jsp"),
    @Result(name = "simpleInput", location = "/gzcgwsp/ywcmn/lhsp/lhsp-info.jsp"),
	@Result(name = "tosimpleInput", location ="/gzcgwsp/ywcmn/lhsp/toLhsp-info.jsp")
})
public class ProcessInstanceAction extends ExtCrudActionSupport<LhspSendItemNoticeForm> {

    @Autowired
    private IProcessInstanceService processInstanceService;
    @Autowired
	private IWfBusTemplateService wfBusTemplateService;	
	@Autowired
	private IWfService wfService;
    
    

	private LhspSendItemNoticeForm form;
    private Page<LhspSendItemNoticeForm> page = new Page<LhspSendItemNoticeForm>(15);

    public String listData() throws Exception {
        ExtUtils.initPageFromExtGridParam(getRequest(), page);
        LoginUserForm loginUserForm = this.getLoginUser();
        processInstanceService.search(page, form);
        renderText(Struts2Utils.getJsonString(page, DateUtils.YEAR_MOUTH_DAY_));
        return null;
    }
    
    @Override
    public String delete() throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String list() throws Exception {
        return CrudActionSupport.SUCCESS;
    }
    
    public String info() throws Exception {
        return "DBLCLICK_WIN";
    }
    
    @Override
    protected void prepareModel() throws Exception {
        if (form == null) form = new LhspSendItemNoticeForm();
    }

    @Override
    public String save() throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

    public LhspSendItemNoticeForm getModel() {
        // TODO Auto-generated method stub
        return form;
    }
    
    /**
	 * 去列表
	 * @return
	 * Mar 23, 2013
	 */
	public String toSimpleInput() {
		String templateCode = getRequest().getParameter("templateCode");
		System.out.println("templateCode ======== "+templateCode);
		WfBusTemplateForm template = wfBusTemplateService.getByTemplateCode(templateCode);
		
		String wfDefId = WfUtils.convertToProcessDefinitionId(template.getWfDefKey(), template.getWfDefVersion());
		WfBusInstanceTaskForm instance=new WfBusInstanceTaskForm();
		instance.setActivityName(wfService.getStartTaskActivityName(wfDefId));
		
		//设置流程定义ID
		instance.setProcessDefId(wfDefId);
		
		//设置流程实体的版本
		instance.setVersion(template.getWfDefVersion());
		
		getRequest().setAttribute("instance", instance);
		getRequest().setAttribute("template", template);
		
		
		return "tosimpleInput";
	}
	
	/**
	 * 列表页面
	 * @return
	 * Mar 23, 2013
	 */
	public String simpleInput() {
		String id = getRequest().getParameter("id");
		System.out.println("id = "+id);
		if(StringUtils.isNotBlank(id)){
			form = processInstanceService.get(Long.parseLong(id));
			getRequest().setAttribute("form", form);
		}
		String templateCode=getRequest().getParameter("templateCode");
		WfBusTemplateForm template = wfBusTemplateService.getByTemplateCode(templateCode);		
		String wfDefId = WfUtils.convertToProcessDefinitionId(template.getWfDefKey(), template.getWfDefVersion());
		WfBusInstanceTaskForm instance=new WfBusInstanceTaskForm();
		instance.setActivityName(wfService.getStartTaskActivityName(wfDefId));
		//设置流程定义ID
		instance.setProcessDefId(wfDefId);
		//设置流程实体的版本
		instance.setVersion(template.getWfDefVersion());
		getRequest().setAttribute("instance", instance);
		getRequest().setAttribute("template", template);
		return "simpleInput";
	}
	
	/**
	 * 接口六：获取办理意见
	 * 根据项目立案编号及部门编号获取所有部门办理意见
	 * 
	 * @return  返回 xml字符串
	 */
	public String  GetDF_Opinion() throws Exception{
		String InstanceID = getRequest().getParameter("InstanceID");
		System.out.println("InstanceID = " + InstanceID);
		ProcessInstanceClient processInstanceClient = new ProcessInstanceClient();
		String str = processInstanceClient.GetDF_Opinion(InstanceID);
		HttpServletResponse response=ServletActionContext.getResponse();
		System.out.println("str = "+str);
		response.getWriter().print(str);
		return null;
	}
	
	/**
	 * 接口一：办理进度公开接口
	 * 根据项目立案编号及部门编号获取当前步骤及属性
	 * 
	 * @return  返回 xml字符串
	 */
	public String  GetProjectStep() throws Exception{
		String InstanceID = getRequest().getParameter("InstanceID");
		System.out.println("InstanceID = " + InstanceID);
		ProcessInstanceClient processInstanceClient = new ProcessInstanceClient();
		String str = processInstanceClient.GetProjectStep(InstanceID,"CHENGGUANWEI");
		HttpServletResponse response=ServletActionContext.getResponse();
		System.out.println("str = "+str);
		response.getWriter().print(str);
		return null;
	}
	
	/**
	 * 接口二：工程信息共享接口
	 * 根据项目立案编号获取阶段一表制信息
	 * 
	 * @return  返回 xml字符串
	 */
	public String  GetProjectForm() throws Exception{
		String InstanceID = getRequest().getParameter("InstanceID");
		System.out.println("InstanceID = " + InstanceID);
		ProcessInstanceClient processInstanceClient = new ProcessInstanceClient();
		String str = processInstanceClient.GetProjectForm(InstanceID);
		HttpServletResponse response=ServletActionContext.getResponse();
		System.out.println("str = "+str);
		response.getWriter().print(str);
		return null;
	}
	
	/**
	 * 接口三：材料信息共享接口
	 * 获取材料的信息
	 * 
	 * @return  返回 xml字符串
	 */
	public String  GetResourcesObjectInfo() throws Exception{
		String InstanceID = getRequest().getParameter("InstanceID");
		System.out.println("InstanceID = " + InstanceID);
		ProcessInstanceClient processInstanceClient = new ProcessInstanceClient();
		String str = processInstanceClient.getSqcl(InstanceID, "DianZ", "CHENGGUANWEI");
		HttpServletResponse response=ServletActionContext.getResponse();
		System.out.println("str = "+str);
		response.getWriter().print(str);
		return null;
	}

	public LhspSendItemNoticeForm getForm() {
		return form;
	}

	public void setForm(LhspSendItemNoticeForm form) {
		this.form = form;
	}

	public Page<LhspSendItemNoticeForm> getPage() {
		return page;
	}

	public void setPage(Page<LhspSendItemNoticeForm> page) {
		this.page = page;
	}
}
